package it.jack;

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import database.*;
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

import oggetti.Utente;

public class Login extends JPanel {

    private static final long serialVersionUID = -2464384911645119427L;
    private JTextField login;
    private JButton accedi;
    private JLabel pass_label;
    private JLabel log_label;
    private JButton esci;
    private JPasswordField pass;

    public Login() {
        initGUI();
        addListener();
        keyListener();
    }

    private void goHome(boolean admin) {
        Main.getInstance().remove(Main.getInstance().getContentPane().getComponent(1));
        Main.getInstance().add(new PannelloPrincipale(admin), BorderLayout.CENTER);
        Main.getInstance().setSize(Toolkit.getDefaultToolkit().getScreenSize());
        Main.getInstance().centra();
        Main.getInstance().setTitle("Pannello Principale");
    }
    private void keyListener(){
       pass.addKeyListener(new KeyAdapter() {

            @SuppressWarnings("static-access")
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    accedi();
                }
            }
        });
        login.addKeyListener(new KeyAdapter() {

            @SuppressWarnings("static-access")
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    accedi();
                }
            }
        });
    }
    private void accedi(){
        String a = login.getText();
                @SuppressWarnings("deprecation")
                String b = pass.getText();
                if (a.length() >0 && b.length() >0) {
                    try {
                        Utente user = new DB().getUtente(a, b);
                        if (user != null) {
                            JOptionPane.showMessageDialog(null, "Accesso effettuato!");
                            Main.getInstance().setUser(user);
                            boolean admin;
                            if (a.equals("admin")) {
                                admin = true;
                            } else {
                                admin = false;
                            }
                            goHome(admin);
                        } else {
                            JOptionPane.showMessageDialog(null, "Login e/o password errati!");
                        }
                    } catch (HeadlessException e) {
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "I campi sono obbligatori!");
                }
    }
    private void addListener() {
        esci.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                int i = JOptionPane.showConfirmDialog(null, "Sei Sicuro di voler uscire?", "Uscita", JOptionPane.YES_NO_OPTION);
                if (i == 0) {
                    System.exit(0);
                }
            }
        });

        accedi.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                accedi();
            }
        });
    }

    private void initGUI() {
        AnchorLayout thisLayout = new AnchorLayout();
        try {
            /*for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }*/
            UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
        }
	        
        setLayout(thisLayout);
        {
            pass_label = new JLabel();
            this.add(pass_label, new AnchorConstraint(429, 456, 505, 84, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            pass_label.setText("Password:");
            pass_label.setPreferredSize(new java.awt.Dimension(91, 190));
        }
        {
            log_label = new JLabel();
            add(log_label, new AnchorConstraint(228, 418, 307, 128, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            log_label.setText("Login:");
            log_label.setPreferredSize(new java.awt.Dimension(87, 22));
        }
        {
            esci = new JButton();
            add(esci, new AnchorConstraint(850, 681, 940, 261, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            esci.setText("Esci");
            esci.setPreferredSize(new java.awt.Dimension(126, 25));
        }
        {
            accedi = new JButton();
            add(accedi, new AnchorConstraint(642, 681, 735, 261, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            accedi.setText("Accedi");
            accedi.setPreferredSize(new java.awt.Dimension(126, 26));
        }
        {
            pass = new JPasswordField();
            add(pass, new AnchorConstraint(426, 938, 516, 508, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            pass.setPreferredSize(new java.awt.Dimension(129, 23));
        }
        {
            login = new JTextField();
            add(login, new AnchorConstraint(232, 938, 322, 508, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            login.setPreferredSize(new java.awt.Dimension(129, 23));
        }

    }
}
